package com.tbit.uqbike.service.business.impl;

import com.github.pagehelper.PageHelper;
import com.tbit.uqbike.object.pojo.Eventdetail;
import com.tbit.uqbike.object.pojo.InviteNewEvent;
import com.tbit.uqbike.service.business.InviteNewEventService;
import com.tbit.uqbike.webmanager.dao.core.CouponDao;
import com.tbit.uqbike.webmanager.dao.core.EventdetailDao;
import com.tbit.uqbike.webmanager.dao.core.InviteNewEventDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 活动表(InviteNewEvent)表服务实现类
 *
 * @author jing
 * @since 2020-12-30 17:31:17
 */
@Service("inviteNewEventService")
public class InviteNewEventServiceImpl implements InviteNewEventService {
    @Resource
    private InviteNewEventDao inviteNewEventDao;
    @Resource
    private CouponDao couponDao;
    @Resource
    private EventdetailDao eventdetailDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public InviteNewEvent queryById(Integer id) {
        return this.inviteNewEventDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<InviteNewEvent> queryAllByLimit(int offset, int limit) {
        return this.inviteNewEventDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param inviteNewEvent 实例对象
     * @return 实例对象
     */
    @Override
    public InviteNewEvent insert(InviteNewEvent inviteNewEvent) {
        this.inviteNewEventDao.insert(inviteNewEvent);
        return inviteNewEvent;
    }

    /**
     * 修改数据
     *
     * @param inviteNewEvent 实例对象
     * @return 实例对象
     */
    @Override
    public InviteNewEvent update(InviteNewEvent inviteNewEvent) {
        this.inviteNewEventDao.update(inviteNewEvent);
        return this.queryById(inviteNewEvent.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.inviteNewEventDao.deleteById(id) > 0;
    }

    @Override
    public List<InviteNewEvent> queryByInviteNewEvent(InviteNewEvent inviteNewEvent) {
        PageHelper.startPage(inviteNewEvent.getPage().getPageNO(), inviteNewEvent.getPage().getRowCount());
        List<InviteNewEvent> inviteNewEvents = inviteNewEventDao.queryByInviteNewEvent(inviteNewEvent);
        return inviteNewEvents;
    }

    @Override
    public void bindingActivity(InviteNewEvent inviteNewEvent) {
        Eventdetail eventdetail = new Eventdetail(Integer.parseInt(inviteNewEvent.getUserCouponName()), inviteNewEvent.getUserNum(), Integer.parseInt(inviteNewEvent.getOldCouponName()), inviteNewEvent.getOldNum());
        eventdetailDao.insert(eventdetail);
        inviteNewEvent.setDetailId(eventdetail.getDetailId());
        inviteNewEventDao.update(inviteNewEvent);
    }

    @Override
    public void untieActivity(InviteNewEvent inviteNewEvent) {
        Integer detailId = inviteNewEvent.getDetailId();
        inviteNewEvent.setDetailId(0);
        inviteNewEventDao.update(inviteNewEvent);
        eventdetailDao.deleteById(detailId);
    }

    @Override
    public void updateActivity(InviteNewEvent inviteNewEvent) {
        Eventdetail eventdetail = new Eventdetail(inviteNewEvent.getDetailId(), Integer.parseInt(inviteNewEvent.getUserCouponName()), inviteNewEvent.getUserNum(), Integer.parseInt(inviteNewEvent.getOldCouponName()), inviteNewEvent.getOldNum());
        eventdetailDao.update(eventdetail);
    }

    @Override
    public Eventdetail selectActivity(InviteNewEvent inviteNewEvent) {
        InviteNewEvent inviteNewEvent1 = inviteNewEventDao.queryById(inviteNewEvent.getId());
        Eventdetail eventdetail = new Eventdetail();
        if (inviteNewEvent1 != null) {
            Integer detailId = inviteNewEvent1.getDetailId();
            eventdetail = eventdetailDao.queryById(detailId);
            if (null != eventdetail) {
                eventdetail.setUserCouponName(couponDao.getByCouponId(eventdetail.getUserCouponId()).getName());
                eventdetail.setOldCouponName(couponDao.getByCouponId(eventdetail.getOldCouponId()).getName());
            }
        }
        return eventdetail;
    }
}